<?php

declare (strict_types=1);

namespace App\Model;

use Hyperf\Database\Model\Relations\HasMany;

/**
 * @property int $pay_order_id
 * @property string $pay_order_sn
 * @property string $pay_outer_sn
 * @property string $pay_type
 * @property string $pay_method
 * @property string $paid_price
 * @property string $refund_fee
 * @property int $pay_time
 * @property int $refund_time
 */
class FlyPayOrder extends Model
{
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $primaryKey = 'pay_order_id';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_pay_order';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['pay_order_id' => 'integer', 'user_id' => 'string', 'pay_time' => 'integer', 'refund_time' => 'integer'];

    /**
     * 获取支付单关联的店铺订单
     * @return HasMany
     */
    public function with_shop_order()
    {
        return $this->hasMany(FlyOrder::class, 'pay_order_id', 'pay_order_id')
            ->with(['with_products', 'with_delivery_config', 'with_shop']);
    }

    /**
     * 关联支付单包含商品
     * @return HasMany
     */
    public function with_products()
    {
        return $this->hasMany(FlyOrderProduct::class, 'pay_order_id', 'pay_order_id')
            ->leftJoin('fly_sku', 'fly_order_product.sku_id', '=', 'fly_sku.sku_id')
            ->select(['fly_order_product.*', 'fly_sku.sku_id as sku_can_use']);
    }
}